package com.xfhy.wallpaperdemo.live;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioAttributes;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.xfhy.wallpaperdemo.DaemonLog;
import com.xfhy.wallpaperdemo.IntentUtils;
import com.xfhy.wallpaperdemo.RomUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;

/**
 * @author : xfhy
 * Create time : 2020/10/30 13:41
 * Description :
 */
public class CoreService extends BaseService implements pk.b, Handler.Callback {
    public static final String c = "intent";
    public static final String d = "start_activity";
    public static final int e = 1;
    public static final long f = TimeUnit.MINUTES.toMillis(1);
    public MediaPlayer a;
    public Handler b;

    public class a implements MediaPlayer.OnErrorListener {
        public a() {
        }

        @Override
        public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
            DaemonLog.d(CoreService.this.getMyName() + " player onError");
            return false;
        }
    }

    public class b implements MediaPlayer.OnCompletionListener {
        public b() {
        }

        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.start();
        }
    }

    public class c implements Runnable {
        public c() {
        }

        @Override
        public void run() {
            CoreService.this.stopSelf();
        }
    }

    private void a() {
        try {
            if (this.a == null || !this.a.isPlaying()) {
                b();
            }
        } catch (Throwable unused) {
        }
    }

    @SuppressLint("WrongConstant")
    private void b() {
        DaemonLog.d(getMyName() + " startPlay called");
        MediaPlayer mediaPlayer = this.a;
        if (mediaPlayer != null) {
            try {
                mediaPlayer.release();
            } catch (Exception unused) {
                DaemonLog.d(getMyName() + " release-1 onError");
            }
        }
        MediaPlayer mediaPlayer2 = new MediaPlayer();
        this.a = mediaPlayer2;
        mediaPlayer2.setOnErrorListener(new a());
        this.a.setWakeMode(getApplicationContext(), 1);
        this.a.setOnCompletionListener(new b());
        try {
            AssetFileDescriptor openFd = getApplicationContext().getAssets().openFd("clean.mp3");
            this.a.setDataSource(openFd.getFileDescriptor(), openFd.getStartOffset(), openFd.getLength());
            this.a.setVolume(1.0f, 1.0f);
            if (RomUtil.isHuaWei() && Build.VERSION.SDK_INT >= 21) {
                this.a.setAudioAttributes(new AudioAttributes.Builder().setUsage(11).build());
            }
            this.a.prepare();
            this.a.start();
            DaemonLog.d(getMyName() + " startPlay success");
        } catch (IOException e2) {
            DaemonLog.d(getMyName() + " error", e2);
        }
    }

    private void c() {
        MediaPlayer mediaPlayer = this.a;
        if (mediaPlayer != null) {
            try {
                mediaPlayer.stop();
                this.a.release();
                this.a = null;
            } catch (Exception e2) {
                DaemonLog.d(getMyName() + " error", e2);
            } catch (Throwable th) {
                this.a = null;
                throw th;
            }
            this.a = null;
        }
    }

    public static void start(Context context) {
        try {
            context.startService(new Intent(context, CoreService.class));
        } catch (Exception unused) {
        }
        ProtectService.start(context);
    }

    /**
     * 由锁屏开启的该核心Service
     */
    public static void startForLockScreen(Context context, Intent intent) {
        IntentUtils.startActivitySafe(context, intent);
        Intent intent2 = new Intent(context, CoreService.class);
        if (intent != null) {
            intent2.putExtra("intent", intent);
            intent2.setAction(d);
        }
        try {
            context.startService(intent2);
        } catch (Exception e2) {
            DaemonLog.d("startService onError", e2);
        }
    }

    @Override
    public boolean handleMessage(@NonNull Message message) {
        if (message.what == 1) {
            DaemonLog.d(getMyName() + " handleMessage stopPlay");
            c();
        }
        return true;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        this.b = new SafeHandler(this);
        pk.getInstance().addCallback(this);
        if (!RomUtil.isMiui()) {
            b();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        pk.getInstance().removeCallback(this);
        MediaPlayer mediaPlayer = this.a;
        if (mediaPlayer != null) {
            try {
                mediaPlayer.release();
            } catch (Exception e2) {
                DaemonLog.d("mPlayer release error", e2);
            }
        }
    }

    @Override
    public void onScreenStatusChanged(boolean z2) {
        DaemonLog.d(getMyName() + " onScreenStatusChanged->" + z2);
        if (z2) {
            this.b.removeMessages(1);
            a();
        } else if (RomUtil.isOppo()) {
            this.b.sendEmptyMessageDelayed(1, f);
        }
    }

    @SuppressLint("WrongConstant")
    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        Intent intent2;
        if (intent == null || !d.equals(intent.getAction()) || (intent2 = (Intent) intent.getParcelableExtra("intent")) == null) {
            return 1;
        }
        boolean isOppo = RomUtil.isOppo();
        if (isOppo) {
            b();
        }
        IntentUtils.startActivitySafe(getApplicationContext(), intent2);
        DaemonLog.d(getMyName() + " startActivity,targetIntent=" + intent2);
        if (!isOppo) {
            return 1;
        }
        new Handler(Looper.getMainLooper()).postDelayed(new c(), 5000);
        return 1;
    }
}

